package com.atguigu.system.service.impl;

import com.atguigu.model.dto.SysDeptDTO;
import com.atguigu.model.system.SysDept;
import com.atguigu.system.mapper.SysDeptMapper;
import com.atguigu.system.service.SysDeptService;
import com.atguigu.system.utils.DeptHelper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author quzengpeng
 * @descpription:
 * @date 2023/3/26
 */
@Slf4j
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

    @Resource
    private SysDeptMapper sysDeptMapper;

    /**
     * 机构列表(树形)
     * @return
     */
    @Override
    public List<SysDeptDTO> findNodes() {
        Wrapper<SysDept> queryWrapper = new QueryWrapper<>();
        List<SysDept> list = sysDeptMapper.selectList(queryWrapper);
        List<SysDeptDTO>  resultList = new ArrayList<>();
        for (SysDept dept : list) {
            SysDeptDTO dto = new SysDeptDTO();
            dto.setId(dept.getId());
            dto.setLabel(dept.getName());
            dto.setParentId(dept.getParentId());
            resultList.add(dto);
        }
        //转树形结构
        resultList = DeptHelper.buildTree(resultList);
        log.info("机构树形结构 {}",resultList);
        return resultList;
    }
}
